<template>
  <j-modal :title="title" :width="800" :visible="visible" switchFullscreen @cancel="handleCancel" :footer="null">
    <a-form-model :model="form" :rules="rules" :label-col="{ span: 3 }" :wrapper-col="{ span: 17 }">
      <a-row>
        <a-col>
          <a-form-model-item label="主题" prop="motif">
            <a-input v-model="form.motif" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col>
          <a-form-model-item label="媒体类别" prop="generalOne" v-if="type === '1'">
            <a-select v-model="form.generalOne" placeholder="请选择" :disabled="isDisabled" style="width: 100%" allowClear>
              <a-select-option v-for="(item, key) in mediaType" :key="key" :value="item.value"
                >{{ item.title }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="占比（%）" prop="generalOne" v-else-if="type === '2'">
            <a-input v-model="form.generalOne" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
          <a-form-model-item label="阅读量（万）" prop="generalOne" v-else-if="type === '3'">
            <a-input v-model="form.generalOne" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
          <a-form-model-item label="宣传地点" prop="generalOne" v-else-if="type === '4'">
            <a-input v-model="form.generalOne" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
          <a-form-model-item label="领导类别" prop="generalOne" v-else-if="type === '5'">
            <a-select v-model="form.generalOne" placeholder="请选择" :disabled="isDisabled" style="width: 100%" allowClear>
              <a-select-option v-for="(item, key) in leaderType" :key="key" :value="item.value"
                >{{ item.title }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="答题分数" prop="generalOne" v-else-if="type === '6'">
            <a-input v-model="form.generalOne" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
          <a-form-model-item label="类型" prop="generalOne" v-else-if="type === '7'">
            <a-select v-model="form.generalOne" placeholder="请选择" :disabled="isDisabled" style="width: 100%" allowClear>
              <a-select-option v-for="(item, key) in registerType" :key="key" :value="item.value"
                >{{ item.title }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item label="注册比例" prop="generalThree" v-if="type === '7'">
            <a-input v-model="form.generalThree" :disabled="isDisabled" placeholder="请输入"> </a-input>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col>
          <a-form-model-item label="附件">
            <j-upload v-model="formData.fileList"></j-upload>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col>
          <a-form-model-item label="描述" prop="mediaGatherInfo">
            <a-textarea v-model="form.mediaGatherInfo" :rows="8" placeholder="请输入"></a-textarea>
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row v-if="type === '审核'">
        <a-col>
          <a-form-model-item label="审核意见" prop="khbz">
            <a-textarea v-model="form.advice" :rows="8" placeholder="请输入"></a-textarea>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <div class="footerBox">
      <a-button class="btn" @click="handleCancel">取消</a-button>
      <a-button class="btn" @click="onOk" style="margin-left: 8px" type="primary">保存</a-button>
      <a-button class="btn" @click="onSubmit" style="margin-left: 8px" type="primary">提交</a-button>
    </div>
  </j-modal>
</template>

<script>
import moment from 'moment'
import Api from '@/api/sjsk/performanceService'
import { getDictItemsFromCache } from '@/api/api'

export default {
  data() {
    return {
      moment,
      title: null,
      type: null,
      visible: false,
      isDisabled: false,
      form: {},
      mediaType: getDictItemsFromCache('mediaType'),
      leaderType:getDictItemsFromCache('leaderType'),
      registerType:getDictItemsFromCache('registerType'),
      url: {
        upload: '/sys/common/upload',
        list: '/sys/oss/file/list',
        delete: '/sys/oss/file/delete',
        minioUpload: '/sys/upload/uploadMinio',
      },
      tokenHeader: null,
      formData: {
        fileList: []
      }
    }
  },
  computed: {
    uploadAction() {
      return window._CONFIG['domianURL'] + this.url.upload
    },
  },

  created() {},
  mounted() {},

  methods: {
    show(_type,row) {
      let title = null
      switch (_type) {
        case '1':
          title = '各类媒体发刊情况'
          break
        case '2':
          title = '关注中国禁毒的粉丝指数'
          break
        case '3':
          title = '@中国禁毒在线发布微博情况'
          break
        case '4':
          title = '开展“禁毒宣传进万家”活动情况'
          break
        case '5':
          title = '开展“领导干部讲禁毒”活动情况'
          break
        case '6':
          title = '全国青少年禁毒知识竞赛情况'
          break
        case '7':
          title = '全国青少年毒品预防教育数字化平台注册情况'
          break
        case '详情': // 详情
          title = '主题详情'
          break
        case '审核': // 详情
          title = '数据填报审核'
          break
        default:
          title = '编辑'
          break
      }
      this.type = _type
      this.title = title
      this.visible = true
      if(_type === '详情' || _type === '审核' || _type === 'edit'){ // 详情或修改
        this.getDetail(row)
      }
    },
    // 主题详情
    getDetail(row){
      let form = {
        id:row.id,
      }
      this.loading = true
      Api.dataFillMotifDetail(form)
        .then((res) => {
          if (res.success) {
            this.form = res.result;
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 保存
    onOk() {
      if(!this.form.motif){
        return this.$message.warning('主题名称不能为空')
      }
      if(!this.form.generalOne){
        return this.$message.warning('请先填写完整信息')
      }
      if(this.type === '审核' && !this.form.advice){
        return this.$message.warning('审核意见不能为空')
      }
      let params = {
        ...this.form
      }
      let interfaceMethod = null
      if(this.type === 'edit'){
        interfaceMethod = Api.updateDataFillMotif(params)
      }else if(this.type === '审核'){
        interfaceMethod = Api.examineDataFillMotif(params)
      }else{
        interfaceMethod = Api.addDataFillMotif(params)
      }
      this.loading = true
      interfaceMethod(params)
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.visible = false
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 提交
    onSubmit() {
      if(!this.form.motif){
        return this.$message.warning('主题名称不能为空')
      }
      if(!this.form.generalOne){
        return this.$message.warning('请先填写完整信息')
      }
      let params = {
        ...this.form
      }
      this.loading = true
      Api.examineDataFillMotifSub(params)
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.visible = false
          } else {
            this.$message.error(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    // 取消
    handleCancel() {
      this.visible = false
    },
    beforeUpload(file) {
      var fileType = file.type
      if (fileType === 'image') {
        if (fileType.indexOf('image') < 0) {
          this.$message.warning('请上传图片')
          return false
        }
      } else if (fileType === 'file') {
        if (fileType.indexOf('image') >= 0) {
          this.$message.warning('请上传文件')
          return false
        }
      }
      return true
    },
    handleChange(info) {
      if (info.file.status === 'done') {
        if (info.file.response.success) {
          this.loadData()
          this.$message.success(`${info.file.name} 上传成功!`)
        } else {
          this.$message.error(`${info.file.response.message}`)
        }
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.response.message}`)
      }
    },

  },
}
</script>

<style lang="less" scoped>
.footerBox {
  display: flex;
  justify-content: flex-end;
  width: 100%;
  padding-top: 20px;
  border-top: 1px solid #e8e8e8;
}
</style>
